#include <machine/asmdefs.h>

ASM_HEADER

FNDEFN_BEGIN(demod5_5mbps_asm)

	stw		%r0,%r1,-4
	stw		%r4,%r1,-8
	stw		%r5,%r1,-12
	stw		%r6,%r1,-16

/* put siAccumI,Q in r8,r3 */
        luh             %r0,%r8,0
        luh             %r5,%r9,0
        luh             %r4,%r8,2
        luh             %r6,%r9,2       ||      mulss       %r3,%r0,%r5
        stuh            %r0,%r9,0       ||      mulss       %r5,%r4,%r5
        stuh            %r4,%r9,2       ||      mulss       %r8,%r4,%r6

        add             %r8,%r3,%r8
        mulss           %r4,%r0,%r6     ||      lb          %r6,%r14,0

        lir             %r9,0

/* put abs(siAccumI) in r8 */
        cmpgt           0,%cf0,%r8,%r9
        jc              1,%cf0,SET_I    ||      subf        %r3,%r4,%r5
        subf            %r8,%r8,%r9
SET_I:
/* put abs(siAccumQ) in r3 */
        cmpgt           0,%cf1,%r3,%r9
        jc              1,%cf1,SET_Q
        subf            %r3,%r3,%r9
SET_Q:
        cmpgt           0,%cf2,%r8,%r3
        jc              1,%cf2,I_BIG

Q_BIG:
        jc              1,%cf1,END_SET_TEMP  ||      lir         %r0,2

        j               END_SET_TEMP    ||      lir         %r0,1

I_BIG:
        jc              1,%cf0,END_SET_TEMP ||      lir         %r0,0

        lir             %r0,3

END_SET_TEMP:

        add             %r12,%r12,%r6

        xor             %r0,%r0,%r10    ||        lb    %r12,%r12,0
        sli             %r11,%r11,2
        or              %r0,%r11,%r0

        xor             %r12,%r0,%r12

        shri            %r6,%r6,4       ||        lwl   %r3,%r1,40
        sli             %r0,%r0,3
        or              %r6,%r6,%r0

        cmpei           0,%cf0,%r10,0   ||        lb   %r5,%r3,0
        jc              1,%cf0,END_BITCOUNT ||  lir    %r4,0

        lir             %r4,4
END_BITCOUNT:

        shl             %r10,%r12,%r4   ||        lw    %r11,%r15,0
        or              %r5,%r5,%r10    ||        stb   %r6,%r14,0

        andil           %r8,%r11,0xF    ||        stb   %r5,%r3,0
        xor             %r8,%r8,%r12    ||	  lw	%r0,%r1,-4
        sli             %r8,%r8,2       ||        lw	%r4,%r1,-8
        add             %r13,%r13,%r8   ||        lw	%r5,%r1,-12

        shri            %r11,%r11,4     ||        lw    %r13,%r13,0

        lw              %r6,%r1,-16

        xor             %r11,%r11,%r13
        stw             %r11,%r15,0	||	ji		%jt0,8

FNDEFN_END(demod5_5mbps_asm)
